package org.intrasearch.frontend.views;

import com.google.inject.Inject;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.intrasearch.spider.SpiderScheduler;
import org.intrasearch.spider.VirtualFiles;

/**
 * PATISDG 10.11.2009
 */
public class StartIndexingPage extends MasterPageIndex
{
    @Inject
    private SpiderScheduler spiderScheduler;

    private String indexLocation;
    private String user;
    private String password;
    private Form form;
    private Label statusLabel;

    public StartIndexingPage()
    {
        super();
        PropertyModel<String> indexLocationModel = new PropertyModel<String>(this, "indexLocation");
        this.form = new Form("startForm")
        {
            @Override
            protected void onSubmit()
            {
                startIndexing();
            }
        };
        this.form.add(new TextField<String>("indexLocation", indexLocationModel));
        form.add(new TextField<String>("user", new PropertyModel<String>(this, "user")));
        form.add(new PasswordTextField("password", new PropertyModel<String>(this, "password")));
        add(this.form);
        statusLabel = new Label("statusLabel", "Indexing started...");
        statusLabel.setVisible(false);
        add(statusLabel);
    }

    public String getIndexLocation()
    {
        return indexLocation;
    }

    public void setIndexLocation(String indexLocation)
    {
        this.indexLocation = indexLocation;
    }

    public String getUser()
    {
        return user;
    }

    public void setUser(String user)
    {
        this.user = user;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    private void startIndexing()
    {
        if (indexLocation != null && indexLocation.length() > 0)
        {
            form.setVisible(false);
            statusLabel.setVisible(true);
            spiderScheduler.scheduleIndexing(VirtualFiles.createFromPathOrUrl(indexLocation, user, password));
        }
    }
}
